<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report extends User_Controller
{
	private $response = array();
	private $userModel;
	private $clientModel;
    public function __construct()
    {
        parent::__construct();
        $this->userModel = $this->load->model('User_Model');
        $this->clientModel = $this->load->model('Client_Model');
        $this->payrollModel = $this->load->model('Payroll_Model');
        $this->timesheetModel = $this->load->model('Timesheet_Model');
        $this->reportModel = $this->load->model('Report_Model');
        $this->load->model('Holiday_Model');
        $this->load->helper('RequestManager');
        $this->load->helper('PHPExport');
        $this->requestManager = RequestManager::getInstance();
        $this->settings = $this->config->item('settings');
        
    }

	public function export($api,$payroll_period_id, $template='payslip', $user_id = 0){
		$data = array();
		
		switch($template){
			case 'employee_timesheet':
				if($payroll_period_id > 0 && $user_id > 0) {
					$result = $this->reportModel->getEmployeeTimesheetData($api,$payroll_period_id, $user_id);	
					$user_profile = $this->userModel->getUserProfile($api, $user_id);
					$params['employee_info'] = $user_profile[0];
					//var_dump($user_profile);
					//echo "$payroll_period_id, $user_id";
					//var_dump($result);	
					//die();
				}
				//$result = array();
				$params['label'] = array('Date','Time In','Time Out','Reg.Hours', 
				'Reg.UT',
				'Reg.OT','Reg.ND',
				'Restday','Excess on RD',
				'Restday ND','SpeH',
				'Excess on SpeH','SpeH ND',
				'SpeH on RD','Excess SpeH on RD',
				'SpeH RD ND','Legal Holiday',
				'Excess LH', 'LH ND',
				'LH on RD','Excess LH on RD', 
				'LH on RD ND', 'LH Unworked');
				
				$params['info'] = array('creator_name'=>'Jomel',
				'current_user'=>'Jomel', 'description'=>'Employee Timesheet',
				'title'=>'Employee Timesheet','subject'=>'Employee Timesheet','keywords'=>'Employee Timesheet',
				'category'=>'Employee Timesheet');
				
				//$params['employee_info'] = array('firstname'=>'Jomel','lastname'=>'Vicente','id_number'=>'12345');
				break;
			case 'payroll_journal':
				if($payroll_period_id > 0) {
					$result = $this->reportModel->getPayrollJournalData($api,$payroll_period_id);		
				}
				$params['label'] = array('No.',
				'Employee Name', 'Reg.Days/Hours',
				'','Amount','','Reg.OT','','Reg.ND',
				'','Restday','','Excess on RD',
				'','Restday ND','','SpeH',
				'','Excess on SpeH','','SpeH ND',
				'','SpeH on RD','','Excess SpeH on RD',
				'','Legal Holiday','','Excess LH',
				'','LH Unworked','','LH on RD',
				'','Excess LH on RD','', 'LH with ND',
				'Adjustment/CA','SSS Maternity/Sickness/QuitClaims/Incentives/13th Month',
				'Gross','Deduction','Total','NET TOTAL');
				
				$params['deductions'] = array('SSS EE','SSS ER','ECC','SSS TOTAL','PHIC EE','PHIC ER','PHIC TOTAL',
				'HDMF EE','HDMP ER','HDMF TOTAL','LOANS','CANTEEN 1', 'CANTEEN 2', 'PENTAX','PENTAX','ADJUSTMENT','UNIFORM/ID','OTHERS','DEDUCTION');
			
				$params['fields'] = array('regular_hours',
				'regular_overtime','regular_night_differential','restday','excess_restday','restday_night_differential',
				'special_holiday','excess_special_holiday','special_holiday_night_differential',
				'special_holiday_restday','excess_special_holiday_restday','legal_holiday',
				'excess_legal_holiday','legal_holiday_unwork','legal_holiday_restday','excess_legal_holiday_restday',
				'legal_holiday_night_differential','adjustment','incentives','approved_undertime','approved_late');
				
				$params['info'] = array('creator_name'=>'Jomel',
				'current_user'=>'Jomel', 'description'=>'Payroll',
				'title'=>'Payroll','subject'=>'Payroll','keywords'=>'Payroll',
				'category'=>'Payroll');
				
				break;
			case 'payslip':
				
				if($payroll_period_id > 0) {
					$result = $this->reportModel->getPayrollJournalData($api,$payroll_period_id, $user_id);		
				}
				
							
				$params['label'] = array('Regular Hours',
				'Regular Overtime', 'Reg. Night Diff',
				'Restday','Excess on Restday','Restday Night Diff',
				'Special Holiday','Excess on Special Holiday','Special Holiday Night Diff',
				'Special Holiday on Restday','Excess Special Holiday on Restday',
				'Legal Holiday','Excess on Legal Holiday','Legal Holiday Unwork',
				'Legal Holiday on Restday','Excess Legal Holiday on Restday',
				'Legal Holiday Night Diff','Adjustment/CA','Incentives/Maternity/Sickness Reimbursement',
				'Undertime','Late');
				
				$params['fields'] = array('regular_hours',
				'regular_overtime','regular_night_differential','restday','excess_restday','restday_night_differential',
				'special_holiday','excess_special_holiday','special_holiday_night_differential',
				'special_holiday_restday','excess_special_holiday_restday','legal_holiday',
				'excess_legal_holiday','legal_holiday_unwork','legal_holiday_restday','excess_legal_holiday_restday',
				'legal_holiday_night_differential','adjustment','incentives','approved_undertime','approved_late');
				
				$params['info'] = array('creator_name'=>'Jomel',
				'current_user'=>'Jomel', 'description'=>'Payroll',
				'title'=>'Payroll','subject'=>'Payroll','keywords'=>'Payroll',
				'category'=>'Payroll');
				break;
				
			
			
		}
		
		$period = $this->payrollModel->getPayrollPeriod($api,$payroll_period_id);
		$params['period'] = array('from'=>$period->date_start, 'to'=>$period->date_end);
		$params['payroll'] = $result;
		
		$phpExport = PHPExport::getInstance();
		$phpExport->setFormat('excel');
		$phpExport->setData($params); 
		$phpExport->setTemplate($template);
		$phpExport->send();

		die();

	}
	
	
	public function view_user($api,$payroll_period_id,$user_id){
		$data = array();
		
		// lookup payroll available
		$user_payroll = $this->payrollModel->userPayroll($api, $payroll_period_id, $user_id);
		$member_info = $this->userModel->getUserProfile($api, $user_id);
		$payroll = $this->payrollModel->getPayrollPeriod($api,$payroll_period_id);
		$user_contributions = $this->userModel->getContributions($api,$user_id);
		//$absences = $this->timesheetModel->getNumberOfAbsences($api, $payroll_period_id, $user_id);
		//var_dump($absences);

		if((count($user_payroll) == 0)){
			$result = $this->timesheetModel->getSummaryLogsByUser($api, $payroll_period_id, $user_id);
			//var_dump($result);
		//	$absences = $this->timesheetModel->getNumberOfAbsences($api, $payroll_period_id, $user_id);
			
			//$member_info = $this->userModel->getUserProfile($api, $user_id);
			$special_holiday = $this->timesheetModel->getHolidayByType($api, $payroll_period_id, $user_id,'special');
			$regular_holiday = $this->timesheetModel->getHolidayByType($api, $payroll_period_id, $user_id,'regular');
			if(!empty($regular_holiday)){
				$regular_worked_holidays = $this->timesheetModel->getMemberWorkedHolidays($regular_holiday, $user_id);
			}
			
			if(!empty($special_holiday)){
				$special_worked_holidays = $this->timesheetModel->getMemberWorkedHolidays($special_holiday, $user_id);
			}
	
			
			if($result != 0 ){
				//$response["message"] = "success";
				$response["data"] = $result[0];
				
				$response["regular_holiday"] = isset($regular_worked_holidays)?$regular_worked_holidays:0;
				$response["special_holiday"] = isset($special_worked_holidays)?$special_worked_holidays:0;
				$response["rest_day"] = $this->timesheetModel->getUserRestDay($api, $payroll_period_id, $user_id);
				// save data to payroll
				$inserted_id = $this->payrollModel->save($response,$payroll_period_id,$user_id);
				$response["user_payroll_id"] = $inserted_id;
				
				// call again
				if($inserted_id > 0 ){
					$response = array();
					$user_payroll = $this->payrollModel->userPayroll($api, $payroll_period_id, $user_id);
					$response['user_payroll'] = $user_payroll[0];
				}
				
			}
		}else {
			//$response['message'] = "success";
			$response['user_payroll'] = $user_payroll[0];
		}
		
		$response['message'] = "success";
		$response["member"] = $member_info[0];
		$response["period"] = $payroll;
		$response["contributions"] = $user_contributions;
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('payroll', $data);
	}
	
	public function client_payroll_periods($api){
		$data = array();
		$result = $this->payrollModel->getClientPayrollPeriods($api);
		if(isset($result) && is_array($result)){
			$response["message"] = "success";
			$response["data"] = $result;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		$this->load->view('payroll', $data);
	}
	
	public function update_field($api){
		$data = array();
		$response["message"] = "success";
		$params = array("{$_POST['field_id']}"=>$_POST['field_value']);
		if(is_numeric($_POST['field_value'])){
			$this->payrollModel->updateField($api,$params, $_POST['payroll_id'], $_POST['user_id'], $_POST['pid']);
		}
		
		$response["field_value"] = floatval($_POST['field_value']);
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('payroll', $data);
	}
	
	public function save_payroll_amount($api){
		if($this->input->post()){
			$params = $this->input->post();
			
			if($params['status'] == 'confirmed' || $params['status'] == 'submitted') {
				// update payroll table
				
				$prm['status'] = $params['status'];
				$prm['hdmf'] = $params['hdmf'];
				$prm['philhealth'] = $params['philhealth'];
				$prm['sss_loan'] = $params['sss_loan'];
				$prm['sss'] = $params['sss'];
				$prm['other_deductions'] = $params['other_deductions'];
				$prm['hdmf_loan'] = $params['hdmf_loan'];
				$prm['tax'] = $params['tax'];
				$prm['user_id'] = $params['user_id'];
				$prm['net_pay'] = $params['net_pay'];
				$prm['gross_salary'] = $params['gross_salary'];
				$return = $this->payrollModel->saveOrUpdatePayroll($api, $prm, $params['pid']);
			} 
			
			if($params['status'] == 'submitted') {
				unset($params['status']);
				unset($params['user_id']);
				unset($params['payroll_period_id']);
				$return = $this->payrollModel->saveOrUpdatePayrollAmount($api, $params);
			}
			
			$response["message"] = "success";
			$response["data"] = $params;
			$data["response"] = ObjectUtils::arrayToJSON($response);
			$this->load->view('payroll', $data);
		}
	}
	
}